package com.example.demo.module.mq;

import com.example.demo.service.shop.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author longzhonghua
 * @data 2/25/2019 7:40 PM
 */
@Component
public class CancelOrderReceiver {
    @Resource
    private OrderService orderService;
  
    
    @RabbitListener(queues = "delay_queue_1")
    public void receive(String orderNo) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("Receiver :执行取消订单:"+orderNo+sdf.format(new Date()));
        try{
        	orderService.cancelOrder(orderNo);//JPA乐观锁实现
        	//mybatis 乐观锁实现
        	//int reuslt = orderService.cancelOrder(orderNo);
        	//if(result==0) throw new Exeception("取消订单订单异常")
        } catch (Exception e) {
        	System.out.println("取消订单：【" + orderNo + "】失败");
        	//保存失败订单记录到数据库
		}
    }
}
